package com.xunyin.modules.recharge.rest;

import com.xunyin.api.ApiResult;
import com.xunyin.api.XunyinException;
import com.xunyin.enums.ErrorEnum;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.mainpart.service.TMainPartService;
import com.xunyin.modules.mainpart.vo.TMainPartOnlyNameVo;
import com.xunyin.modules.recharge.domain.TRechargeSet;
import com.xunyin.modules.recharge.param.RechargeParam;
import com.xunyin.modules.recharge.service.TRechargeOrderService;
import com.xunyin.modules.recharge.service.TRechargeSetService;
import com.xunyin.modules.recharge.vo.TRechargeOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 充值优惠套餐控制器
 * </p>
 *
 * @author admin
 * @since 2022-10-24
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "充值优惠套餐", tags = "共享空间:充值优惠套餐")
public class RechargeController {

    private final TMainPartService tMainPartService;

    private final TRechargeSetService tRechargeSetService;

    private final TRechargeOrderService tRechargeOrderService;

    /**
     * 根据会客厅ID获取充值优惠信息
     */
    @AppLog(value = "根据会客厅ID获取充值优惠信息", type = 1)
    //@AuthCheck
    @PostMapping("/getRechargeOptionlist")
    @ApiOperation(value = "根据会客厅ID获取充值优惠信息", notes = "根据会客厅ID获取充值优惠信息")
    public ApiResult<List<TRechargeSet>> getRechargeOptionlist(@Validated @RequestBody RechargeParam queryParam) throws IOException {
        Integer teaHouseId = queryParam.getTeaHouseId();
        if (teaHouseId == null) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        }

        String userPhone = queryParam.getUserPhone();
        String appId = queryParam.getAppId();
        log.info("getInfoTea -> appId={}", appId);
        TMainPartOnlyNameVo tMainPartOnlyNameVo = tMainPartService.getMainPartByAppId(queryParam.getAppId());
        if (tMainPartOnlyNameVo == null) {
            //return ApiResult.fail(ErrorEnum.E_90006.getErrorMsg())
            throw new XunyinException(ErrorEnum.E_90006.getErrorMsg());
        }
        List<TRechargeSet> list = tRechargeSetService.getRechargeOptionlist(teaHouseId, userPhone, tMainPartOnlyNameVo.getId());
        if (list == null) {
            throw new XunyinException(ErrorEnum.E_90004.getErrorMsg());
        } else {
            return ApiResult.ok(list);
        }
    }


    /**
     * 查询充值订单
     */
    @AppLog(value = "查询充值订单", type = 1)
    //@AuthCheck
    @PostMapping("/getRechargeOrderByTeaHouseId")
    @ApiOperation(value = "查询充值订单", notes = "查询充值订单")
    public ApiResult<List<TRechargeOrderVo>> getRechargeOrderByTeaHouseId(@Validated @RequestBody RechargeParam queryParam) {

        return ApiResult.ok(tRechargeOrderService.getRechargeOrderByTeaHouseId(queryParam.getTeaHouseId()));

    }
}
